package com.biomatch.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.biomatch.domain.Card;
import com.biomatch.dto.Pagination;
import com.biomatch.response.RestResponse;
import com.biomatch.response.RestResult;
import com.biomatch.service.CardService;
import com.biomatch.utils.Internationalize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("dance.card")
@RequestMapping("/card")
@Api(value = "街舞课表管理")
public class CardController {
    @Autowired
    private Internationalize internationalize;

    @Autowired
    private CardService cardService;

    @RequestMapping(value="/getAllByPage", method = RequestMethod.POST)
    @ApiOperation(value = "分页获取课卡信息", response = Card.class, httpMethod = "POST")
    public RestResult<Card> getAllByPage(@RequestBody Pagination pagination) {
        IPage<Card> result= cardService.getAllByPage(pagination);
        return RestResponse.success(result);
    }

    @RequestMapping(value="/getAll", method = RequestMethod.POST)
    @ApiOperation(value = "获取全部", response = Card.class, httpMethod = "POST")
    public RestResult<List<Card>> getAll() {
        List<Card> result =cardService.getAll();
        return RestResponse.success(result);
    }

    @RequestMapping(value="/saveOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "新增或更新课卡", response = Card.class, httpMethod = "POST")
    public RestResult<Card> saveOrUpdate(@RequestBody Card card) {
        Card result = cardService.saveOrUpdate(card);
        if (result == null) {
            return RestResponse.error(internationalize.getMessage("position.name.repeat"));
        } else {
            return RestResponse.success(result);
        }
    }

    @RequestMapping(value = "/stopCard",method = RequestMethod.POST)
    @ApiOperation(value = "停卡", response = Card.class, httpMethod = "POST")
    public RestResult<Card> stopCard(@RequestBody Card card) {
        Card result = cardService.stopCard(card);
        if (result == null) {
            return RestResponse.error(internationalize.getMessage("position.name.repeat"));
        } else {
            return RestResponse.success(result);
        }
    }

    @RequestMapping(value = "/openCard",method = RequestMethod.POST)
    @ApiOperation(value = "开卡", response = Card.class, httpMethod = "POST")
    public RestResult<Card> openCard(@RequestBody Card card) {
        Card result = cardService.openCard(card);
        if (result == null) {
            return RestResponse.error(internationalize.getMessage("position.name.repeat"));
        } else {
            return RestResponse.success(result);
        }
    }


    @RequestMapping(value="/delete", method = RequestMethod.POST)
    @ApiOperation(value = "删除课卡", response = Card.class, httpMethod = "POST")
    public RestResult<Card> delete(@RequestBody Card card ) {
        Card result = cardService.delete(card);
        if (result == null) {
            return RestResponse.error(internationalize.getMessage("position.status.used"));
        } else {
            return RestResponse.success(result);
        }
    }

}
